package top.ucat.x.system.service.management;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ucat.box.starter.mybatis.plus.base.service.BaseCrudServiceImpl;
import top.ucat.x.system.dao.api.SysRolePermissionDao;
import top.ucat.x.system.entity.SysMenu;
import top.ucat.x.system.entity.SysRolePermission;
import top.ucat.x.system.entity.SysUserRole;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: Jun
 * @Date: 2020/4/21 17:43
 * @Description:
 */
@Service
public class SysRolePermissionServiceImpl extends BaseCrudServiceImpl<SysRolePermission> implements SysRolePermissionService {


    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysRolePermissionDao sysRolePermissionDao;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public void saveRolePermissions(List<SysRolePermission> permissions) {
        if (permissions.isEmpty()) {
            return;
        }
        SysRolePermission sysRolePermission = permissions.get(0);
        String roleId = sysRolePermission.getRoleId();
        Integer type = sysRolePermission.getType();
        //删除原先的权限
        sysRolePermissionService.deleteRolePermissions(roleId, type);
        //批量增加
        permissions.forEach(permission -> {
            sysRolePermissionDao.save(permission);
        });
    }

    @Override
    public void deleteRolePermissions(String roleId, Integer type) {
        LambdaQueryWrapper<SysRolePermission> wrapper = sysRolePermissionDao.getWrapper().lambda()
                .eq(SysRolePermission::getRoleId, roleId)
                .eq(SysRolePermission::getType, type);
        sysRolePermissionDao.remove(wrapper);
    }

    @Override
    public List<SysRolePermission> getRolePermissions(String roleId, Integer type) {
        LambdaQueryWrapper<SysRolePermission> wrapper = sysRolePermissionDao.getWrapper().lambda()
                .eq(SysRolePermission::getRoleId, roleId)
                .eq(SysRolePermission::getType, type);
        List<SysRolePermission> list = sysRolePermissionDao.list(wrapper);
        return list;
    }

    @Override
    public List<SysRolePermission> getUserPermission(String userId) {
        List<SysUserRole> userRoles = sysUserRoleService.getUserRoles(userId);
        List<String> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysRolePermission> wrapper = sysRolePermissionDao.getWrapper().lambda()
                .in(SysRolePermission::getRoleId, roleIds);
        List<SysRolePermission> list = sysRolePermissionDao.list(wrapper);
        return list;
    }

    @Override
    public Set<String> getUserPermissionCodes(String userId) {

        //获取用户的权限
        List<SysRolePermission> userPermissions = this.getUserPermission(userId);
        //获取权限内的菜单
        List<SysMenu> menuList = sysMenuService.getUserPermissionMenus(userId);

        if (menuList != null && menuList.size() > 0) {
            //分组key:menuId,v:菜单权限码
            Map<String, String> menuMap = menuList.stream().
                    filter(m -> StringUtils.isNotBlank(m.getPermissionCode()))
                    .collect(Collectors.toMap(SysMenu::getId, SysMenu::getPermissionCode));

            //拼装权限码
            Set<String> permissionCodeList = userPermissions.stream().
                    filter(sysRolePermission -> StringUtils.isNotEmpty(menuMap.get(sysRolePermission.getMenuId())))
                    //菜单权限码+"_"+权限类型
                    .map(sysRolePermission -> menuMap.get(sysRolePermission.getMenuId()) + "_" + sysRolePermission.getType())
                    .collect(Collectors.toSet());
            return permissionCodeList;
        }
        return new HashSet<>();
    }
}
